package com.framework.dubbo.producer.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Charsets;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 描述：
 * 将px pt转换为rem
 * @author xianggj
 * @Date 2021/11/1 10:31
 **/
public class ConverRemUtils {

    private static final String HREADER = "html{font-size:200%}";
    public static void main(String[] args) {
        TimeInterval timer = DateUtil.timer();
        File file = new File("D:\\sinux\\pdf\\pdf.css");
        String fileStr = FileUtil.readString(file, Charsets.UTF_8);
        Pattern patternAll = getPatternAll();
        String[] split = StrUtil.split(fileStr, "\n");
        System.out.println(timer.interval() + "ms");
        String collect = Arrays.stream(split).parallel().map(e -> {
            if (e.startsWith("@font-face")) {
                return e;
            } else {
                return pxToEm(patternAll, e);
            }
        }).collect(Collectors.joining("\n"));
        System.out.println(timer.interval() + "ms");
        FileUtil.writeString(collect, "D:\\sinux\\pdf\\pdf1.css", Charsets.UTF_8);
        System.out.println(timer.interval() + "ms");
    }


    /**
     * 转换
     * @param file 源文件
     * @param newPath 新路径
     */
    public static void conver(File file, String newPath){
        TimeInterval timer = DateUtil.timer();
        String fileStr = FileUtil.readString(file, Charsets.UTF_8);
        Pattern patternAll = getPatternAll();
        String[] split = StrUtil.split(fileStr, "\n");
        String collect = Arrays.stream(split).parallel().map(e -> {
            //长文本不转换
            if (e.startsWith("@font-face")) {
                return e;
            } else {
                return pxToEm(patternAll, e);
            }
        }).collect(Collectors.joining("\n"));
        FileUtil.writeString(collect, newPath, Charsets.UTF_8);
        System.out.println("转换em耗时："+timer.interval() + "ms");
    }


    private static Pattern getPatternAll() {
        String pattern = "(:)((-?[1-9]\\d*\\.?\\d*)|(-?0\\.\\d*))(px|pt)";
        Pattern compile = Pattern.compile(pattern);
        return compile;
    }
    private static String pxToEm(Pattern compile, String value) {
        List<String> allGroups = ReUtil.findAll(compile, value,0);
        for (String ptStr : allGroups) {
            String substring = StringUtils.substring(ptStr, 1, -2);
            Double aDouble = Double.valueOf(substring);
            if (ptStr.endsWith("pt")){
                aDouble = aDouble *4/3;
            }
            Double newDouble = aDouble/16;
            String newStr = ":"+String.format("%.6f", newDouble) + "rem";
            value = value.replace(ptStr, newStr);
        }
        return value;
    }
}
